package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Reference
    private AdminService adminService;
    @Reference
    private PermissionService permissionService;
    //    @RequestMapping("/")
//    public String index(){
//        return "frame/index";
//    }
//    去首页
    @RequestMapping("/")
    public String index(Map map) {
//        设置默认的用户id，超级管理员
//        Long userId=1L;
//        Admin admin = adminService.getById(userId);
//        通过SpringSecurity
         User user= (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//         根据用户名获取Admin对象的方法
        Admin admin = adminService.getAdminByUsername(user.getUsername());
//        根据用户id获取用户权限菜单的方法
        List<Permission> permissionList=permissionService.getMenuPermissionByAdminId(admin.getId());
        map.put("admin",admin);
        map.put("permissionList",permissionList);
        return "frame/index";
    }
//  去主页面
    @RequestMapping("/main")
    public String main() {
        return "frame/main";
    }
//    去登录页面
    @RequestMapping("/login")
    public String login(){
        return "frame/login";
    }
//    去没有权限的页面
    @RequestMapping("/auth")
    public String auth(){
        return "frame/auth";
    }
}
